package com.csun.cmny;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-11-22 14:41
 * @Description:
 */

import com.csun.cmny.provider.resolver.CmnyLocaleResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import tk.mybatis.spring.annotation.MapperScan;

import javax.validation.Validator;
import java.util.Locale;

/**
 * The class cmny cloud uac application.
 */
@Slf4j
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
@MapperScan("com.csun.cmny.provider.dao")
public class CmnyProviderUacApplication {

    public static void main(String[] args) {
        SpringApplication.run(com.csun.cmny.CmnyProviderUacApplication.class, args);
        log.info("spring cloud cmny provider uac application 启动!");
    }

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        Locale.setDefault(Locale.CHINA);    // classpath:org/springframework/security/messages_zh_CN
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:org/springframework/security/messages",
                "i18n/errors/ErrorMessages", "i18n/prompt/PromptMessages",
                "i18n/validation/ValidationMessages", "classpath:ValidationMessages",
                "classpath:ErrorCodeMessages");
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver() {
        return new CmnyLocaleResolver();
    }

    @Bean
    public Validator getValidator() throws Exception {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource());
        return validator;
    }

    @Bean
    public CmnyLocaleResolver cmnyLocaleResolver() {
        return new CmnyLocaleResolver();
    }

}